* This script contains the code to replicate TableD for the online appendix in Ductor, L., Fafchamps, M., Goyal S. and M. van der Leij. Social Networks and Research Output. The Review of Economics and Statistics.

log using TablesD.log, replace
use prodndnp_fullsample.dta
set matsize 800
set more off
/*******Seemingly Unrelated Regressions**************/

/*MODEL 0*/

reg lprodf3 y2-y27 nopapers t2-t27 if group==1
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf3<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrolsM0= (lprodf3-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrolsM0)if ybols<.  /*RSS of the predicted model*/
quietly sum peols    /*summarizing the RSS*/
quietly scalar rssols=r(max)  /*obtaining the total RSS*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols

/*MODEL 1*/

reg lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 if group==1
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf3<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrolsM1= (lprodf3-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrolsM1)if ybols<.  /*RSS of the predicted model*/
quietly sum peols    /*summarizing the RSS*/
quietly scalar rssols=r(max)  /*obtaining the total RSS*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols


/**********MODEL 3. SURE regressions, row coauthors' productivity**********/
sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lnetprod1y-lnetprod12y) (lnetprod3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l9 lnetprod1y-lnetprod15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(12)
drop sqerrsureg diffsqerrlM3

/*******MODEL 3. SURE regressions, row coauthors' coauthors productivity*******/
sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lnetprod21y-lnetprod211y) (lnetprod23yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l9 lnetprod21y-lnetprod215y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(11)
drop sqerrsureg diffsqerrlM3


/*MODEL 3. SURE regressions, row DEGREE*/



/*SUREG out-of-sample program*/
sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 degree1y-degree6y) (degree3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 degree1y-degree15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(6)
drop sqerrsureg diffsqerrlM3

/*MODEL 3. SURE regressions, row degree of order 2*/
sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 degree21y-degree25y) (degree23yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 degree21y-degree213y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(5)
drop sqerrsureg diffsqerrlM3


/*MODEL 3. SURE regressions, row giant component*/


/*SUREG out-of-sample program*/
sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 gc1y-gc8y) (gc3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 gc1y-gc15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(8)
drop sqerrsureg diffsqerrlM3


/*MODEL 3. SURE regressions, row betweenness*/

sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 gc1y-gc9y lbet1y-lbet9y) (lbet3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l11 gc1y-gc15y lbet1y-lbet15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(9)
drop sqerrsureg diffsqerrlM3


/*MODEL 3. SURE regressions, row closeness*/

sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 gc1y-gc10y lclos1y-lclos10y) (lclos3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 gc1y-gc15y lclos1y-lclos15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(10)
drop sqerrsureg diffsqerrlM3


/*MODEL 3. SURE regressions, row working with a top 1%*/

gen neiq1fs3yf3=F3.neiq1fs3y

sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 neiq1fs1y-neiq1fs13y) (neiq1fs3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l5 neiq1fs1y-neiq1fs15y)  if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrsureg diffsqerrlM3

/*******MULTIVARIATE MODEL 3. SUREG***********/

sureg (lprodf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lnetprod1y-lnetprod8y lnetprod21y-lnetprod28y degree1y-degree8y degree21y-degree28y gc1y-gc8y lbet1y-lbet8y lclos1y-lclos8y neiq1fs1y-neiq1fs8y) (lnetprod3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l9 lnetprod1y-lnetprod10y lnetprod21y-lnetprod210y degree1y-degree10y degree21y-degree210y gc1y-gc10y lbet1y-lbet10y lclos1y-lclos10y neiq1fs1y-neiq1fs10y) (lnetprod23yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l5 lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y) (degree3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 lnetprod1y-lnetprod13y lnetprod21y-lnetprod213y degree1y-degree13y degree21y-degree213y gc1y-gc13y lbet1y-lbet13y lclos1y-lclos13y neiq1fs1y-neiq1fs13y) (degree23yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 lnetprod1y-lnetprod12y lnetprod21y-lnetprod212y degree1y-degree12y degree21y-degree212y gc1y-gc12y lbet1y-lbet12y lclos1y-lclos12y neiq1fs1y-neiq1fs12y) (gc3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 lnetprod1y-lnetprod15y lnetprod21y-lnetprod215y degree1y-degree15y degree21y-degree215y gc1y-gc15y lbet1y-lbet15y lclos1y-lclos15y neiq1fs1y-neiq1fs15y) (lbet3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l4 lnetprod1y-lnetprod15y lnetprod21y-lnetprod215y degree1y-degree15y degree21y-degree215y gc1y-gc15y lbet1y-lbet15y lclos1y-lclos15y neiq1fs1y-neiq1fs15y) (lclos3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l2 lnetprod1y-lnetprod5y lnetprod21y-lnetprod25y degree1y-degree5y degree21y-degree25y gc1y-gc5y lbet1y-lbet5y lclos1y-lclos5y neiq1fs1y-neiq1fs5y) (neiq1fs3yf3=y2-y27 nopapers t2-t27 lprodf3l-lprodf3l6 lnetprod1y-lnetprod6y lnetprod21y-lnetprod26y degree1y-degree6y degree21y-degree26y gc1y-gc6y lbet1y-lbet6y lclos1y-lclos6y neiq1fs1y-neiq1fs6y)if group==1
qui scalar n1=e(N_1)
scalar rmsesureg=e(rmse_1)
predict ysureg if group==2 & lprodf3<., xb equation(#1)
scalar r2i1sure= e(r2_1)   /*computing the R2 of the baseline model*/
qui gen sqerrsureg= (lprodf3-ysureg)^2 if ysureg<.
quietly egen pesureg=sum(sqerrsureg)if ysureg<.  /*RSS of the predicted model*/
quietly sum pesureg    /*summarizing the RSS*/
quietly scalar rsssureg=r(max)  /*obtaining the total RSS*/
quietly scalar nsureg=r(N)    /*number of observations in group 2*/
scalar rmseo2sureg=(rsssureg/nsureg)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2sureg "  SUREG out-of-sample RMSE"
di nsureg " Number of observations in the out-of-sample group"
drop pesureg ysureg

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrsureg-sqerrolsM1
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(8)
drop sqerrsureg diffsqerrlM3


log close

